<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="药品名称" prop="medicineCnName">
        <el-input v-model="queryParams.medicineCnName" placeholder="请输入药品汉字名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="药品成分" prop="medicineIngredient">
        <el-input v-model="queryParams.medicineIngredient" placeholder="请输入药品成分" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="药品性状" prop="medicineAppearance">
        <el-input v-model="queryParams.medicineAppearance" placeholder="请输入药品性状" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="功能主治" prop="purposeSimple">
        <el-input v-model="queryParams.purposeSimple" placeholder="请输入功能主治简称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出二维码</el-button>
      </el-form-item>

      <!-- <el-col :span="1.5" style="float: right;">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAccount"></el-button>
      </el-col> -->
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:meidicineInfo:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:meidicineInfo:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:meidicineInfo:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:meidicineInfo:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5" style="float: right;">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAccount"></el-button>
      </el-col>
    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> 
    </el-row> -->

    <el-table v-loading="loading" :data="meidicineInfoList" @selection-change="handleSelectionChange"
      ref="multipleTable" tooltip-effect="dark" style="width: 100%">
      <el-table-column type="selection" width="85"></el-table-column>
      <!-- <el-table-column label="${comment}" align="center" prop="id" /> -->
      <el-table-column label="药品名称" align="center" prop="medicineCnName" />
      <el-table-column label="药品规格" align="center" prop="medicineSpecifications" />
       <!--<el-table-column label="药品性状" align="center" prop="medicineAppearance" /> -->

      <!-- <el-table-column label="功能主治" align="center" prop="purpose" />
      <el-table-column label="功能主治简称" align="center" prop="purposeSimple" /> -->
      <el-table-column label="二维码地址" align="center" prop="qrCodeUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.qrCodeUrl" :width="50" :height="50" />
        </template>
      </el-table-column>

      <el-table-column label="添加日期" align="center" prop="checkDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button size="mini" type="text" @click="downloadImage(scope.row)"
            v-hasPermi="['system:meidicineInfo:remove']">导出</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改meidicineInfo对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      基础、使用、成产
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="药品汉字名称" prop="medicineCnName">
          <el-input v-model="form.medicineCnName" placeholder="请输入药品汉字名称" />
        </el-form-item>
        <el-form-item label="药品拼音" prop="medicinePyName">
          <el-input v-model="form.medicinePyName" placeholder="请输入药品拼音" />
        </el-form-item>
        <el-form-item label="药品logo" prop="logo">
          <image-upload v-model="form.logo" />
        </el-form-item>
        <el-form-item label="核准日期" prop="checkDate">
          <el-date-picker clearable v-model="form.checkDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择核准日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="药品修改时间" prop="mixDate">
          <el-date-picker clearable v-model="form.mixDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择药品修改时间">
          </el-date-picker>
        </el-form-item>

        <h3 style="font-weight: bold;">说明书详情</h3>
        <el-form-item label="药品成分" prop="medicineIngredient">
          <el-input style="width: 80%;" v-model="form.medicineIngredient" placeholder="请输入药品成分" />

          <el-button size="mini" style="margin-left: 10px;" type="text">试听</el-button>
        </el-form-item>
        <el-form-item label="药品性状" prop="medicineAppearance">
          <el-input style="width: 80%;" v-model="form.medicineAppearance" placeholder="请输入药品性状" />
          <el-button size="mini" style="margin-left: 10px;" type="text">试听</el-button>
        </el-form-item>
        <el-form-item label="药品规格" prop="medicineSpecifications">
          <el-input style="width: 80%;" v-model="form.medicineSpecifications" placeholder="请输入药品规格" />
          <el-button size="mini" style="margin-left: 10px;" type="text">试听</el-button>
        </el-form-item>

        <el-form-item label="功能主治简称" prop="purposeSimple">
          <el-input style="width: 80%;" v-model="form.purposeSimple" placeholder="请输入功能主治简称" />
          <el-button size="mini" style="margin-left: 10px;" type="text">试听</el-button>
        </el-form-item>


        <el-form-item label="功能主治">
          <editor v-model="form.purpose" :min-height="192" />
        </el-form-item>

        <!-- <el-form-item label="二维码地址" prop="qrCodeUrl">
          <image-upload v-model="form.qrCodeUrl"/>
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改meidicineInfo对话框 -->
    <el-dialog title="新增药品说明书名称" :visible.sync="openAdd" append-to-body>
      <el-form ref="formAdd" :model="formAdd" :rules="rulesAdd" label-position="top">
        <el-form-item label="新增药品说明书名称" prop="medicineCnName">
          <el-input v-model="formAdd.medicineCnName" placeholder="请输入药品汉字名称" />
        </el-form-item>
        <el-form-item label="药品规格" prop="medicineSpecifications">
          <el-input v-model="formAdd.medicineSpecifications" placeholder="请输入药品规格" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="AddformSubmit">新 建</el-button>
        <el-button @click="Addcancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMeidicineInfo, getMeidicineInfo, delMeidicineInfo, addMeidicineInfo, updateMeidicineInfo } from "@/api/system/meidicineInfo";
import { mount } from "sortablejs";

export default {
  name: "MeidicineInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // meidicineInfo表格数据
      meidicineInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openAdd: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        medicineCnName: null,
        medicineIngredient: null,
        medicineAppearance: null,
        purposeSimple: null,
      },
      // 表单参数
      form: {},
      formAdd: {},
      // 表单校验
      rules: {},
      rulesAdd: {
        medicineCnName: [
          { required: true, trigger: "blur", message: "请输入药品汉字名称" }
        ],
        medicineSpecifications: [
          { required: true, trigger: "blur", message: "请输入药品规格" }
        ]

      }
    };
  },
  created() {
    this.getList();
  },
  activated() {
    this.getList();

  },
  methods: {
    /** 查询meidicineInfo列表 */
    getList() {
      this.loading = true;
      listMeidicineInfo(this.queryParams).then(response => {
        this.meidicineInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    Addcancel() {
      this.openAdd = false;
      this.resetAdd();
    },
    // 表单重置
    resetAdd() {
      this.formAdd = {
        medicineCnName: null,
        medicineSpecifications: null
      };
      this.resetForm("form");
    },
    reset() {
      this.form = {
        id: null,
        medicineCnName: null,
        medicinePyName: null,
        logo: null,
        checkDate: null,
        mixDate: null,
        medicineIngredient: null,
        medicineAppearance: null,
        medicineSpecifications: null,
        purpose: null,
        purposeSimple: null,
        qrCodeUrl: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    downloadImage(row) {
      fetch("https://slhsms.kpc.com.cn/prod-api" + row.qrCodeUrl)
        .then(response => response.blob())
        .then(blob => {
          // 创建一个指向blob的URL
          const objUrl = URL.createObjectURL(blob);

          // 创建一个a标签
          const a = document.createElement('a');
          a.href = objUrl;
          a.download = row.medicineCnName;

          // 触发下载
          document.body.appendChild(a);
          a.click();

          // 清理临时的URL对象
          document.body.removeChild(a);
          URL.revokeObjectURL(objUrl);
        })
        .catch(error => console.error(error));
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      console.log(this.ids);
      
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加药品";
    },
    /** 右新增按钮操作 */
    handleAccount() {
      this.resetAdd();
      this.openAdd = true;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMeidicineInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改meidicineInfo";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMeidicineInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMeidicineInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    AddformSubmit() {
      this.$refs["formAdd"].validate(valid => {
        if (valid) {
          this.openAdd = false
          this.$router.push({
            path: '/instructions',
            query: {
              medicineCnName: this.formAdd.medicineCnName,
              medicineSpecifications: this.formAdd.medicineSpecifications,
            }
          })
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除meidicineInfo编号为"' + ids + '"的数据项？').then(function () {
        return delMeidicineInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      const ids={ids:this.ids }
      this.download('system/meidicineInfo/export', ids, `meidicineInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
